﻿using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Mbs.ComponentModel
{
    public class ComponentObject : LocalizedObject, IEditableObject, IChangeTracking,INotifyPropertyChanged
    {
        
        [field:NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        [Browsable(false)]
        public bool IsChanged { get; private set; }

        /// <summary>
        /// Accept change
        /// </summary>
        public virtual void AcceptChanges()
        {
            IsChanged = false;
        }


        protected virtual void OnPropertyChanged(string propertyName, object newValue, object oldValue)
        {
            IsChanged = true;
            if (IsEdit)
                return;

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName, oldValue, newValue));
        }

        [XmlIgnore]
        [NonSerialized]
        private int _editLevel;


        [Browsable(false)]
        public bool IsEdit
        {
            get { return _editLevel > 0; }
        }

        public void BeginEdit()
        {
            _editLevel++;
        }

        public void CancelEdit()
        {
            _editLevel--;
        }

        public void EndEdit()
        {
            _editLevel--;
        }

        protected override void Dispose(bool disposing)
        {
            PropertyChanged = null;
            base.Dispose(disposing);
        }

        #region IRevertibleChangeTracking Members

        

        #endregion
    }

}
